package com.junxonline.instant.admin.controller.config;

import com.junxonline.instant.dao.entity.model.config.Menu;
import com.junxonline.instant.dao.entity.vo.common.IdsVO;
import com.junxonline.instant.dao.entity.vo.config.MenuVO;
import com.junxonline.instant.admin.service.config.MenuService;
import com.junxonline.instant.common.annotation.OperationLog;
import com.junxonline.instant.common.entity.common.CommonResult;
import com.junxonline.instant.common.enumeration.AdminErrorEnum;
import com.junxonline.instant.common.enumeration.OperationEnum;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 菜单管理Controller
 *
 * @author JunX
 * @date 2021-06-30
 */
@Log4j2
@RestController
@RequestMapping(value = "/config/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 查询上级菜单树（TreeSelect）
     *
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:menu:query")
    @OperationLog(label = "菜单管理-查询上级菜单树", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryParentTree")
    public CommonResult queryParentTree() {
        return CommonResult.success(menuService.queryParentTree());
    }

    /**
     * 查询菜单列表
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:menu:query")
    @OperationLog(label = "菜单管理-查询列表", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryList")
    public CommonResult queryList(@RequestBody(required = false) MenuVO vo) {
        return CommonResult.success(menuService.queryList(vo));
    }

    /**
     * 查询菜单
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:menu:query")
    @OperationLog(label = "菜单管理-查询菜单", type = OperationEnum.QUERY)
    @PostMapping(value = "/query")
    public CommonResult query(@RequestBody(required = false) MenuVO vo) {
        if (vo.getId() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        return CommonResult.success(menuService.query(vo.getId()));
    }

    /**
     * 新增菜单
     *
     * @param menu 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:menu:add")
    @OperationLog(label = "菜单管理-新增菜单", type = OperationEnum.ADD)
    @PostMapping(value = "/add")
    public CommonResult add(@RequestBody(required = false) Menu menu) {
        if (menu == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        menuService.add(menu);
        return CommonResult.success();
    }

    /**
     * 编辑菜单
     *
     * @param menu 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:menu:edit")
    @OperationLog(label = "菜单管理-编辑菜单", type = OperationEnum.EDIT)
    @PostMapping(value = "/edit")
    public CommonResult edit(@RequestBody(required = false) Menu menu) {
        if (menu == null || menu.getId() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        menuService.edit(menu);
        return CommonResult.success();
    }

    /**
     * 删除菜单
     *
     * @param idsVO 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:menu:delete")
    @OperationLog(label = "菜单管理-删除菜单", type = OperationEnum.DELETE)
    @PostMapping(value = "/delete")
    public CommonResult delete(@RequestBody(required = false) IdsVO<List<Long>> idsVO) {
        if (idsVO == null || idsVO.getIds() == null || idsVO.getIds().size() <= 0) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        menuService.delete(idsVO.getIds());
        return CommonResult.success();
    }

}
